var db = require('../db/commonDB')
module.exports = {
    //修改收货地址
    changeAddress: function (address, cb) {
        var sql = `update address set address_name=?,address_phone=?,address_prov=?,address_city=?,address_details=?,address_area=? where user_id = ? and address_id= ?`
        var params = [address.address_name, address.address_phone, address.address_prov, address.address_city, address.address_details, address.address_area, address.user_id, address.address_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查询订单失败！'))
                }
            }
        })
    },
    //添加新收货地址
    addNewAddress: function (address, cb) {
        var sql = `insert into address(address_name,address_id,user_id,address_phone,address_details,address_city,address_prov,address_area) value(?,?,?,?,?,?,?,?)`
        var userId = parseInt(address.user_id)
        var params = [address.address_name, address.address_id, userId, address.address_phone, address.address_details, address.address_city, address.address_prov, address.address_area]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('添加新收货地址失败！'))
                }
            }
        })
    },
    //删除收货地址
    delAddress: function (address, cb) {
        var sql = `delete from address where user_id = ? and address_id = ?`
        var params = [address.user_id, address.address_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除收货地址失败！'))
                }
            }
        })
    },
    //获取全部地址
    viewAddress: function (address, cb) {
        var sql = `select * from address where user_id = ?`
        var params = address.user_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看收货地址失败！'))
                }
            }
        })
    },
    //拿取用户某个地址
    getUserSomeAddress: function (address, cb) {
        var sql = `select * from address where user_id = ? and address_id = ?`
        var params = [address.user_id, address.address_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看收货地址失败！'))
                }
            }
        })
    },
    //设置默认收货地址
    setDefaultAddress: function (address, cb) {
        var sql = `update address set adds_default = 1 where user_id = ? and address_id = ?`
        var params = [address.user_id,address.address_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('修改默认地址失败！'))
                }
            }
        })
    },
    //删除默认收货地址
    delDefaultAddress: function (address, cb) {
        var sql = `update address set adds_default = 0 where user_id = ? and address_id = ?`
        var params = [address.user_id,address.address_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除默认地址失败！'))
                }
            }
        })
    }
}